import React, { Component } from 'react'
import PubSub from 'pubsub-js'
import Far from './components/Far'

export default class App extends Component {
  componentDidMount() {
    // 一般都是在组件挂载成功的时候订阅
    this.token = PubSub.subscribe('xxx', (topic, data) => {
      console.log(topic, data)
    })
    this.yToken = PubSub.subscribe('yyy', (topic, data) => {
      console.log(topic, data)
    })
  }
  componentWillUnmount() {
    //组件卸载时清除自己的订阅
    PubSub.unsubscribe(this.token)
    PubSub.unsubscribe(this.yToken)
  }
  render() {
    return (
      <div>
        <button
          onClick={() => {
            PubSub.unsubscribe(this.token)
          }}
        >
          点击清除app订阅的xxx话题
        </button>
        <button
          onClick={() => {
            PubSub.unsubscribe('xxx')
          }}
        >
          点击清除所有的xxx话题
        </button>
        <button
          onClick={() => {
            PubSub.clearAllSubscriptions()
          }}
        >
          点击清除所有订阅
        </button>
        <Far></Far>
      </div>
    )
  }
}
